home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 1 / Eagles_Nest_Mac_Collection_Disc_1.TOAST / Special Hardware / CoStar202#2 / CoStar Disk 2 / For Programmers Only / CoStar Driver API / CoStarAPI.c next >
C/C++ Source or Header  |  1993-04-26  |  14KB  |  589 lines

  1. #include <Devices.h>
  2.  
  3. //#include    "csCodes.h"
  4. enum {
  5.         csPrintGraphic        = 0x82,
  6.         csGetStatus,
  7.         csGetPrinterList,
  8.         csChoosePrinter,
  9.         csGetCommandList,
  10.         csSendPrinterCommand,
  11.         csStyleDialog,
  12.         csJobDialog,
  13.         csGetPageSizeList,
  14.         csGetPrintRecord,
  15.         csOpenDoc,
  16.         csCloseDoc,
  17.         csSetResolution,
  18.         csGetSuppList,
  19.         csPrValidate,
  20.         csStoreTemplate,
  21.         csSuspendPrinting,
  22.         csResumePrinting,
  23.         csCancelPrinting,
  24.         csGetIcon,
  25.         csDeleteTemplate,
  26.         csChooseTemplates,
  27.         csSetProcPtrs
  28. };
  29.  
  30. #include     "CoStarAPI.h"
  31.  
  32. #define rDriverName    ".CoStarDriver"
  33.  
  34. /**************** Globals *****************************************/
  35. short        gDrvrRefNum;
  36.  
  37. /**************** Declarations ************************************/
  38. void    ClrCntrlParam(CntrlParam *ctlParamP);
  39.  
  40. /**************** Functions ***************************************/
  41. /*
  42.  *    This call returns the refNum of the CoStar driver.
  43.  *        It also places this refNum in the global variable gDrvrRefNum.
  44.  *        This global is used by the rest of the calls, so InitCoStarDriver 
  45.  *        MUST be called before any of the other API calls.
  46.  */
  47. short    InitCoStarDriver()
  48. {
  49. OSErr    err;
  50.  
  51.     err    = OpenDriver("\p.CoStarDriver", &gDrvrRefNum);
  52.  
  53.     if(err)    return(0);
  54.     else    return(gDrvrRefNum);
  55. }
  56.  
  57.  
  58. /*
  59.  *    Returns a handle to the list of printers supported by the INIT.
  60.  */
  61. SuppPrListHandle    GetSuppPrinterList()
  62. {
  63. CntrlParam            ctlParam;
  64. SuppPrListHandle    prListH;
  65.  
  66.     ClrCntrlParam(&ctlParam);
  67.     ctlParam.ioCRefNum    = gDrvrRefNum;
  68.     ctlParam.csCode        = csGetSuppList;
  69.  
  70.     PBControl((ParmBlkPtr ) &ctlParam, false);
  71.  
  72.     prListH    = *((SuppPrListHandle *) &(ctlParam.csParam[0]));
  73.     
  74.     return(prListH);
  75. }
  76.  
  77.  
  78. /*
  79.  *    Returns a handle to the printer list of the type indicated.
  80.  *        It will run a dialog to edit the list if opCode has bit 0 set.
  81.  *        It will only include network printers if opCode has bit 1 set.
  82.  */
  83. PrListHndl    GetPrinterList(PrinterID prID, char opCode)
  84. {
  85. CntrlParam        ctlParam;
  86. PrListHndl        prListH;
  87.  
  88.     ClrCntrlParam(&ctlParam);
  89.     ctlParam.ioCRefNum    = gDrvrRefNum;
  90.     ctlParam.csCode        = csGetPrinterList;
  91.  
  92.     *((PrinterID *) &(ctlParam.csParam[0]))    = prID;
  93.     *((char *) &(ctlParam.csParam[1]))    = opCode;
  94.  
  95.     PBControl((ParmBlkPtr ) &ctlParam, false);
  96.  
  97.     prListH    = *((PrListHndl *) &(ctlParam.csParam[2]));
  98.     
  99.     return(prListH);
  100. }
  101.  
  102. /*
  103.  *    This call is used to indicate which printer will be used for subsequent calls.
  104.   *        It will run a dialog to choose the printer if opCode has bit 0 set.
  105.  *        It will only include network printers if opCode has bit 1 set.
  106.  */
  107. PrinterID    ChoosePrinter(PrinterID prID, char opCode)
  108. {
  109. CntrlParam        ctlParam;
  110.  
  111.     ClrCntrlParam(&ctlParam);
  112.     ctlParam.ioCRefNum    = gDrvrRefNum;
  113.     ctlParam.csCode        = csChoosePrinter;
  114.  
  115.     *((PrinterID *) &(ctlParam.csParam[0]))    = prID;
  116.     *((char *) &(ctlParam.csParam[1]))    = opCode;
  117.  
  118.     PBControl((ParmBlkPtr ) &ctlParam, false);
  119.  
  120.     prID    = *((PrinterID *) &(ctlParam.csParam[2]));
  121.     return(prID);
  122. }
  123.  
  124. /*
  125.  *    Gets a default print handle for the indicated printer type.
  126.  */
  127. THPrint    GetPrintRec(PrinterID printID)
  128. {
  129. THPrint        printH;
  130. CntrlParam    ctlParam;
  131.  
  132.     ClrCntrlParam(&ctlParam);
  133.     ctlParam.ioCRefNum        = gDrvrRefNum;
  134.     ctlParam.csCode            = csGetPrintRecord;
  135.     ctlParam.csParam[0]        = printID;
  136.     PBControl((ParmBlkPtr ) &ctlParam, false);
  137.     
  138.     printH    = *((THPrint *) &(ctlParam.csParam[1]));
  139.     return(printH);
  140. }
  141.  
  142. /*
  143.  *    Runs the style dialog for the passed print record handle.
  144.  *        If updatePrefs is true, this print handle is stored in the prefs file.
  145.  */
  146. Boolean    StlDialog(THPrint printH, Boolean updatePrefs)
  147. {
  148. Boolean        result;
  149. CntrlParam    ctlParam;
  150.  
  151.     ClrCntrlParam(&ctlParam);
  152.     ctlParam.ioCRefNum        = gDrvrRefNum;
  153.     ctlParam.csCode            = csStyleDialog;
  154.     *((THPrint *) &(ctlParam.csParam[0]))    = printH;
  155.     *((Boolean *) &(ctlParam.csParam[2]))    = updatePrefs;
  156.     PBControl((ParmBlkPtr ) &ctlParam, false);
  157.     
  158.     result    = ctlParam.csParam[3];
  159.     return(result);
  160. }
  161.  
  162. /*
  163.  *    Runs the job dialog for the passed print record handle.
  164.  */
  165. Boolean    JobDialog(THPrint printH)
  166. {
  167. Boolean        result;
  168. CntrlParam    ctlParam;
  169.  
  170.     ClrCntrlParam(&ctlParam);
  171.     ctlParam.ioCRefNum        = gDrvrRefNum;
  172.     ctlParam.csCode            = csJobDialog;
  173.     *((THPrint *) &(ctlParam.csParam[0]))    = printH;
  174.     PBControl((ParmBlkPtr ) &ctlParam, false);
  175.     
  176.     result    = ctlParam.csParam[2];
  177.     return(result);
  178. }
  179.  
  180. /*
  181.  *    Validates the passed PREC to insure it's OK for prID.
  182.  */
  183. Boolean    ValidatePrintRec(THPrint printH, PrinterID prID)
  184. {
  185. Boolean        result;
  186. CntrlParam    ctlParam;
  187.  
  188.     ClrCntrlParam(&ctlParam);
  189.     ctlParam.ioCRefNum        = gDrvrRefNum;
  190.     ctlParam.csCode            = csPrValidate;
  191.     *((THPrint *) &(ctlParam.csParam[0]))    = printH;
  192.     ctlParam.csParam[2]                        = prID;
  193.     PBControl((ParmBlkPtr ) &ctlParam, false);
  194.     
  195.     result    = ctlParam.csParam[3];
  196.     return(result);
  197. }
  198.  
  199. /*
  200.  *    Opens a document for spooling pictures.
  201.  */
  202. OSErr    OpenDoc(THPrint printH)
  203. {
  204. OSErr        result;
  205. long        jobID;
  206. CntrlParam    ctlParam;
  207.  
  208.     ClrCntrlParam(&ctlParam);
  209.     ctlParam.ioCRefNum        = gDrvrRefNum;
  210.     ctlParam.csCode            = csOpenDoc;
  211.     *((THPrint *) &(ctlParam.csParam[0]))    = printH;
  212.     PBControl((ParmBlkPtr ) &ctlParam, false);
  213.     
  214.     result    = ctlParam.csParam[2];
  215.     jobID    = *((long *) &(ctlParam.csParam[3]));
  216.     return(result);
  217. }
  218.  
  219. /*
  220.  *    Closes the spooled document.
  221.  */
  222. OSErr    CloseDoc(THPrint printH)
  223. {
  224. OSErr        result;
  225. CntrlParam    ctlParam;
  226.  
  227.     ClrCntrlParam(&ctlParam);
  228.     ctlParam.ioCRefNum        = gDrvrRefNum;
  229.     ctlParam.csCode            = csCloseDoc;
  230.     *((THPrint *) &(ctlParam.csParam[0]))    = printH;
  231.     PBControl((ParmBlkPtr ) &ctlParam, false);
  232.     
  233.     result    = ctlParam.csParam[2];
  234.     return(result);
  235. }
  236.  
  237. OSErr    SuspendPrinting()
  238. {
  239. OSErr        result;
  240. CntrlParam    ctlParam;
  241.  
  242.     ClrCntrlParam(&ctlParam);
  243.     ctlParam.ioCRefNum        = gDrvrRefNum;
  244.     ctlParam.csCode            = csSuspendPrinting;
  245.     PBControl((ParmBlkPtr ) &ctlParam, false);
  246.     
  247.     result    = ctlParam.csParam[0];
  248.     return(result);
  249. }
  250.  
  251. OSErr    ResumePrinting()
  252. {
  253. OSErr        result;
  254. CntrlParam    ctlParam;
  255.  
  256.     ClrCntrlParam(&ctlParam);
  257.     ctlParam.ioCRefNum        = gDrvrRefNum;
  258.     ctlParam.csCode            = csResumePrinting;
  259.     PBControl((ParmBlkPtr ) &ctlParam, false);
  260.     
  261.     result    = ctlParam.csParam[0];
  262.     return(result);
  263. }
  264.  
  265. OSErr    CancelPrinting(long    jobID)
  266. {
  267. OSErr        result;
  268. CntrlParam    ctlParam;
  269.  
  270.     ClrCntrlParam(&ctlParam);
  271.     ctlParam.ioCRefNum        = gDrvrRefNum;
  272.     ctlParam.csCode            = csCancelPrinting;
  273.     *((long *) &(ctlParam.csParam[0]))    = jobID;
  274.     PBControl((ParmBlkPtr ) &ctlParam, false);
  275.     
  276.     result    = ctlParam.csParam[2];
  277.     return(result);
  278. }
  279.  
  280. /*
  281.  */
  282. Handle    GetPrinterIcon(PrinterID prID, short screenDepth)
  283. {
  284. Handle        result;
  285. CntrlParam    ctlParam;
  286.  
  287.     ClrCntrlParam(&ctlParam);
  288.     ctlParam.ioCRefNum        = gDrvrRefNum;
  289.     ctlParam.csCode            = csGetIcon;
  290.     ctlParam.csParam[0]        = prID;
  291.     ctlParam.csParam[1]        = screenDepth;
  292.     PBControl((ParmBlkPtr ) &ctlParam, false);
  293.     
  294.     result    = *((Handle *) &(ctlParam.csParam[2]));
  295.     return(result);
  296. }
  297.  
  298. /*
  299.  */
  300. OSErr    StoreTemplate(PicHandle picH, THPrint printH, Ptr nameP, MailAddrPtr mailAddrP)
  301. {
  302. OSErr        result;
  303. CntrlParam    ctlParam;
  304.  
  305.     ClrCntrlParam(&ctlParam);
  306.     ctlParam.ioCRefNum        = gDrvrRefNum;
  307.     ctlParam.csCode            = csStoreTemplate;
  308.     *((PicHandle *) &(ctlParam.csParam[0]))        = picH;
  309.     *((THPrint *) &(ctlParam.csParam[2]))        = printH;
  310.     *((Ptr *) &(ctlParam.csParam[4]))            = nameP;
  311.     *((MailAddrPtr *) &(ctlParam.csParam[6]))    = mailAddrP;
  312.     PBControl((ParmBlkPtr ) &ctlParam, false);
  313.     
  314.     result    = ctlParam.csParam[8];
  315.     return(result);
  316. }
  317.  
  318. OSErr    DeleteTemplate(Ptr nameP, PrinterID printID)
  319. {
  320. OSErr        result;
  321. CntrlParam    ctlParam;
  322.  
  323.     ClrCntrlParam(&ctlParam);
  324.     ctlParam.ioCRefNum        = gDrvrRefNum;
  325.     ctlParam.csCode            = csDeleteTemplate;
  326.     *((Ptr *) &(ctlParam.csParam[0]))        = nameP;
  327.     *((PrinterID *) &(ctlParam.csParam[2]))    = printID;
  328.     PBControl((ParmBlkPtr ) &ctlParam, false);
  329.     
  330.     result    = ctlParam.csParam[3];
  331.     return(result);
  332. }
  333.  
  334. OSErr    ChooseTemplate(Ptr nameP, PrinterID printID)
  335. {
  336. OSErr        result;
  337. CntrlParam    ctlParam;
  338.  
  339.     ClrCntrlParam(&ctlParam);
  340.     ctlParam.ioCRefNum        = gDrvrRefNum;
  341.     ctlParam.csCode            = csChooseTemplates;
  342.     *((Ptr *) &(ctlParam.csParam[0]))        = nameP;
  343.     *((PrinterID *) &(ctlParam.csParam[2]))    = printID;
  344.     PBControl((ParmBlkPtr ) &ctlParam, false);
  345.     
  346.     result    = ctlParam.csParam[3];
  347.     return(result);
  348. }
  349.  
  350. /*
  351.  *    Spools a label (or labels) for the passed print record handle.
  352.  *        Uses the passed PicHandle for the image to be printed. 
  353.  */
  354. OSErr    PrintGraphic(PicHandle picH, THPrint printH, Ptr tmplNameP, RectListPtr nRectP)
  355. {
  356. OSErr        result;
  357. CntrlParam    ctlParam;
  358.  
  359.     ClrCntrlParam(&ctlParam);
  360.     ctlParam.ioCRefNum        = gDrvrRefNum;
  361.     ctlParam.csCode            = csPrintGraphic;
  362.     *((PicHandle *) &(ctlParam.csParam[0]))        = picH;
  363.     *((THPrint *) &(ctlParam.csParam[2]))        = printH;
  364.     *((Ptr *) &(ctlParam.csParam[4]))            = tmplNameP;
  365.     *((RectListPtr *) &(ctlParam.csParam[6]))    = nRectP;
  366.     PBControl((ParmBlkPtr ) &ctlParam, false);
  367.     
  368.     result    = ctlParam.csParam[8];
  369.     return(result);
  370. }
  371.  
  372. /*
  373.  *    Returns the command list for the indicated printer type.
  374.  */
  375. PrCmdListHndl    GetPrinterCommands(PrinterID prID)
  376. {
  377. CntrlParam        ctlParam;
  378. PrCmdListHndl    prCmdListH;
  379.  
  380.     ClrCntrlParam(&ctlParam);
  381.     ctlParam.ioCRefNum        = gDrvrRefNum;
  382.     ctlParam.csCode            = csGetCommandList;
  383.     *((PrinterID *) &(ctlParam.csParam[0]))    = prID;
  384.     PBControl((ParmBlkPtr ) &ctlParam, false);
  385.  
  386.     prCmdListH    = *((PrCmdListHndl *) &(ctlParam.csParam[1]));
  387.     return(prCmdListH);
  388. }
  389.  
  390. /*
  391.  *    Sends the passed command to the printer.
  392.  */
  393. OSErr    SendPrinterCommand(CommandID theCommand, PrinterID prID)
  394. {
  395. OSErr            err;
  396. CntrlParam        ctlParam;
  397.  
  398.     ClrCntrlParam(&ctlParam);
  399.     ctlParam.ioCRefNum        = gDrvrRefNum;
  400.     ctlParam.csCode            = csSendPrinterCommand;
  401.     *((CommandID *) (ctlParam.csParam))        = theCommand;
  402.     *((PrinterID *) (&ctlParam.csParam[1]))    = prID;
  403.     PBControl((ParmBlkPtr ) &ctlParam, false);
  404.  
  405.     err    = ctlParam.csParam[2];
  406.     return(err);
  407. }
  408.  
  409. /*
  410.  *    Sets resolution of the printH.
  411.  */
  412. void    SetResolution(THPrint printH, Boolean setHigh)
  413. {
  414. CntrlParam    ctlParam;
  415.  
  416.     ClrCntrlParam(&ctlParam);
  417.     ctlParam.ioCRefNum        = gDrvrRefNum;
  418.     ctlParam.csCode            = csSetResolution;
  419.     *((THPrint *) &(ctlParam.csParam[0]))    = printH;
  420.     *((Boolean *) &(ctlParam.csParam[2]))    = setHigh;
  421.     PBControl((ParmBlkPtr ) &ctlParam, false);
  422. }
  423.  
  424. /*
  425.  *    Returns the page size list for the indicated printer type.
  426.  */
  427. PgInfoListHndl    GetPageSizes(PrinterID prID)
  428. {
  429. CntrlParam        ctlParam;
  430. PgInfoListHndl    pgSizeListH;
  431.     
  432.     ClrCntrlParam(&ctlParam);
  433.     ctlParam.ioCRefNum        = gDrvrRefNum;
  434.     ctlParam.csCode            = csGetPageSizeList;
  435.     *((PrinterID *) (ctlParam.csParam))    = prID;
  436.     PBControl((ParmBlkPtr ) &ctlParam, false);
  437.  
  438.     pgSizeListH    = *((PgInfoListHndl *) &(ctlParam.csParam[1]));
  439.     return(pgSizeListH);
  440. }
  441.  
  442. /*
  443.  *    Utility routine for clearing a CntrlParam block.
  444.  */
  445. void    ClrCntrlParam(CntrlParam *ctlParamP)
  446. {
  447. Ptr        charP    = (Ptr ) ctlParamP;
  448. short    length    = sizeof(CntrlParam);
  449.  
  450.     while (length--)
  451.         (*charP++) = 0;
  452. }
  453.  
  454. #if 0
  455. Here are some code fragments as examples of how to use these calls:
  456.  
  457. /*
  458.  *    Print a graphic onto a printer chosen by the user.
  459.  *        picH is some picture you'd like to see on a label.
  460.  */
  461. PrintAGraphic(PicHandle    picH)
  462. {
  463. THPrint        printH;        /* A print record handle                        */
  464. PrinterID    prID;        /* The ID of the printer chosen for this label.    */
  465.  
  466.     InitCoStarDriver();                    /* Get the driver's ID                */
  467.     prID    = ChoosePrinter(0, true);    /* Choose a printer                    */
  468.     printH    = GetPrintRec(prID);        /* Get the default print record.    */
  469.  
  470.     if(StlDialog(printH, true)){        /* Run the style dialog.            */
  471.         if(JobDialog(printH)){            /* Run the job dialog.                */
  472.             PrintGraphic(picH, printH);    /* Print the graphic.                */
  473.         }
  474.     }
  475. }
  476.  
  477. /*
  478.  *    Build a menu from the printer list, 
  479.  *        and allow the user to choose a printer from that menu.
  480.  */
  481. ChoosePrinterMenu(MenuHandle menuH)
  482. {
  483. PrListHndl        prListH;
  484. PrinterSpecPtr    prSpecP;
  485. short            i;
  486.  
  487.     InitCoStarDriver();                /* Get the driver's ID                */
  488.     prListH    = GetPrinterList(prID, false);
  489.  
  490.     HLock((Handle ) prListH);
  491.  
  492.     prSpecP = (*prListH)->PLList;
  493.     /* Fill the menu with the names from the printer list.    */
  494.     for(i = 0; i < (*prListH)->PLCount; i++, prSpecP++){
  495.         InsMenuItem(menuH, "\px", i);
  496.         SetItem(menuH, iFirstPrinter + i, prSpecP->PSName);
  497.     }
  498.  
  499.     HUnlock((Handle ) prListH);
  500.  
  501. ...When the menu is selected, call the choose printer function...
  502. ...itemHit is the menu Item selected...
  503. PrinterSpecPtr    prSpecP;
  504. PrinterID        prID;
  505.  
  506.     prSpecP = &(*prListH)->PLList[itemHit - 1];
  507.     prID    = prSpecP->PSID;                /* The ID of the selected printer.    */
  508.     prID    = ChoosePrinter(prID, false);    /* Choose the printer (without dialog).    */
  509.     
  510. }
  511.  
  512. /*
  513.  *    Build a menu of the printer's commands, 
  514.  *        and allow the user to send a command.
  515.  */
  516. PrinterCommandMenu(MenuHandle menuH, PrinterID prID)
  517. {
  518. PrCmdListHndl    cmdListH;
  519. PrCmdPtr        cmdP;
  520. short            i;
  521.  
  522.     InitCoStarDriver();        /* Get the driver's ID                */
  523.     cmdListH    = GetPrinterCommands(prID);    /* Pass 0 to use default printer.    */
  524.  
  525.     /* Add the names of the commands to the menu.    */
  526.     HLock((Handle ) cmdListH);
  527.     cmdP    = (*cmdListH)->PCList;
  528.     for(i = 1; i <= (*cmdListH)->PCCount; i++, cmdP++){
  529.         InsMenuItem(menuH, "\px", i);
  530.         SetItem(menuH, i, cmdP->PCName);
  531.     }
  532.     HUnlock((Handle ) cmdListH);
  533.  
  534. ...When the menu is selected, call the send command function...
  535. ...itemHit is the menu Item selected...
  536.  
  537.     cmdP    = &(*cmdListH)->PCList[itemHit - 1];
  538.     SendPrinterCommand(cmdP->PCID, prID);        /* Send the command to the printer.    */
  539. }
  540.  
  541. /*
  542.  *    
  543.  */
  544. PrinterTypesMenu()
  545. {
  546. short                i;
  547. SuppPrListHandle    suppPrH;
  548. SuppPrRecPtr        suppPrP;
  549. PrinterID            newPrID;
  550.  
  551.     suppPrH    = GetSuppPrinterList();
  552.     if(suppPrH == nil) return;
  553.     
  554.     HLock((Handle ) suppPrH);
  555.     suppPrP    = (*suppPrH)->SPLList;
  556.     
  557.     for(i = 0; i < (*suppPrH)->SPLCount; i++, suppPrP++){
  558.         InsMenuItem(gTypesMenuH, suppPrP->SPName, 0x7F);
  559.     }
  560.     
  561.     DisposeHandle((Handle ) suppPrH);
  562.  
  563. ...When the menu is selected, change the selected printer type...
  564. ...itemHit is the menu Item selected...
  565. ...gSelPrinterID is a global for the current PrinterID...
  566.  
  567.     suppPrH    = GetSuppPrinterList();            /* Get the list of supported printers.    */
  568.     if(suppPrH == nil) return;
  569.  
  570.     HLock((Handle ) suppPrH);
  571.     suppPrP    = &(*suppPrH)->SPLList[itemHit - 1];
  572.     
  573.     if(PRTYPE(gSelPrinterID) != PRTYPE(suppPrP->SPDefaultID)){
  574.         newPrID        = ChoosePrinter(suppPrP->SPDefaultID, false);
  575.  
  576.         gSelPrinterID    = newPrID;            /* Save the new PrinterID.        */
  577.         CallGetPrinterList(newPrID, false);    /* Get the new printer list.    */
  578. ...Rebuild the printer list menu...
  579.         GetPrinterCommands(newPrID);        /* Get the new command list.    */
  580. ...Rebuild the printer commands menu...
  581.         GetPageSizes(newPrID);                /* Get the new page size list .    */
  582. ...Rebuild the page sizes menu...
  583.     }
  584.  
  585.     DisposeHandle((Handle ) suppPrH);
  586. }
  587.  
  588. #endif
  589.